import { defineStore } from "pinia";

export const cesiumMapStore = defineStore("cesiumMap", {
  state: () => ({
    // 瓦片数据 - 包含完整的瓦片信息
    tiles: [],
    // 当前瓦片数据 - 最新加载的瓦片数据
    currentTileData: null,
    // 瓦片POI数据 - 从瓦片中解析出的POI数据
    tilePOIData: [],
    // 视口内POI的数据 - 当前视口范围内的POI
    viewportPOIData: [],
    // 视口内避让后POI的数据 - 经过碰撞检测避让后的POI
    viewportCollisionAvoidedPOIData: [],
    // 屏幕水平位置 - 鼠标或视口中心的屏幕X坐标
    screenHorizontalPosition: 0,
    // 屏幕垂直位置 - 鼠标或视口中心的屏幕Y坐标
    screenVerticalPosition: 0,
    // 地图层级 - 当前地图的缩放层级
    mapLevel: 0,
    // 鼠标状态 - 包含鼠标位置、按键状态等信息
    mouseStatus: {
      position: { x: 0, y: 0 },
      buttons: {
        left: false,
        middle: false,
        right: false
      },
      isDragging: false,
      dragStartPosition: null,
      wheelDelta: 0,
      pickedObject: null,
      cartesian: null,
      cartographic: null
    },
    // 当前瓦片信息 - 用于显示
    currentTileInfo: {
      tilesLoaded: 0,
      poiCount: 0,
      status: '就绪',
      details: '点击"加载矢量瓦片"开始测试'
    },
    // POI数据
    pois: [],
    // 可见的POI数据
    visiblePOIs: [],
    // 瓦片加载状态
    tileLoadingStatus: {
      isLoading: false,
      loadedCount: 0,
      totalCount: 0,
      error: null
    },
    // 缓存统计信息
    cacheStats: {
      tileCount: 0,
      totalSize: 0,
      averageSize: 0,
      lastCleanTime: null
    }
  }),

  getters: {
    // 获取瓦片总数
    getTileCount: (state) => state.tiles.length,

    // 获取POI总数
    getPOICount: (state) => state.pois.length,

    // 获取瓦片POI数据总数
    getTilePOICount: (state) => state.tilePOIData.length,

    // 获取视口内POI总数
    getViewportPOICount: (state) => state.viewportPOIData.length,

    // 获取视口内避让后POI总数
    getViewportCollisionAvoidedPOICount: (state) => state.viewportCollisionAvoidedPOIData.length,

    // 获取可见POI总数
    getVisiblePOICount: (state) => state.visiblePOIs.length,

    // 获取当前屏幕位置
    getCurrentScreenPosition: (state) => ({
      x: state.screenHorizontalPosition,
      y: state.screenVerticalPosition
    }),

    // 获取当前地图层级
    getCurrentMapLevel: (state) => state.mapLevel,

    // 获取鼠标状态
    getMouseStatus: (state) => state.mouseStatus,

    // 检查鼠标是否按下
    isMousePressed: (state) => (button) => {
      return state.mouseStatus.buttons[button] || false
    },

    // 检查是否正在拖拽
    isDragging: (state) => state.mouseStatus.isDragging,

    // 获取当前瓦片边界信息
    getCurrentTileBounds: (state) => {
      if (state.currentTileData && state.currentTileData.bounds) {
        return state.currentTileData.bounds
      }
      return null
    },

    // 获取当前瓦片层级
    getCurrentTileZoomLevel: (state) => {
      if (state.currentTileData && state.currentTileData.zoomLevel) {
        return state.currentTileData.zoomLevel
      }
      return null
    },

    // 检查是否有瓦片数据
    hasTileData: (state) => state.tiles.length > 0,

    // 检查是否有POI数据
    hasPOIData: (state) => state.pois.length > 0,

    // 检查是否有瓦片POI数据
    hasTilePOIData: (state) => state.tilePOIData.length > 0,

    // 获取瓦片加载状态
    getLoadingStatus: (state) => state.tileLoadingStatus,

    // 获取当前瓦片信息
    getCurrentTileInfo: (state) => state.currentTileInfo
  },

  actions: {
    // 设置瓦片数据
    setTiles (tileData) {
      console.log('设置瓦片数据到 store ->', tileData)
      this.tiles = Array.isArray(tileData) ? tileData : [tileData]

      // 如果是单个瓦片数据对象，设置为当前瓦片数据
      if (!Array.isArray(tileData)) {
        this.currentTileData = tileData
      }

      // 更新加载状态
      this.tileLoadingStatus.isLoading = false
      this.tileLoadingStatus.loadedCount = this.tiles.length
      this.tileLoadingStatus.error = null
    },

    // 设置瓦片POI数据
    setTilePOIData (tilePOIData) {
      console.log('设置瓦片POI数据到 store ->', tilePOIData.length, '个POI')
      this.tilePOIData = Array.isArray(tilePOIData) ? tilePOIData : []
    },

    // 设置视口内POI数据
    setViewportPOIData (viewportPOIData) {
      console.log('设置视口内POI数据到 store ->', viewportPOIData.length, '个POI')
      this.viewportPOIData = Array.isArray(viewportPOIData) ? viewportPOIData : []
    },

    // 设置视口内避让后POI数据
    setViewportCollisionAvoidedPOIData (collisionAvoidedPOIData) {
      console.log('设置视口内避让后POI数据到 store ->', collisionAvoidedPOIData.length, '个POI')
      this.viewportCollisionAvoidedPOIData = Array.isArray(collisionAvoidedPOIData) ? collisionAvoidedPOIData : []
    },

    // 设置屏幕位置
    setScreenPosition (x, y) {
      this.screenHorizontalPosition = x
      this.screenVerticalPosition = y
    },

    // 设置地图层级
    setMapLevel (level) {
      this.mapLevel = level
    },

    // 更新鼠标状态
    updateMouseStatus (mouseStatus) {
      this.mouseStatus = { ...this.mouseStatus, ...mouseStatus }
    },

    // 设置鼠标位置
    setMousePosition (x, y) {
      this.mouseStatus.position = { x, y }
    },

    // 设置鼠标按键状态
    setMouseButtonState (button, pressed) {
      this.mouseStatus.buttons[button] = pressed
    },

    // 设置拖拽状态
    setDragState (isDragging, startPosition = null) {
      this.mouseStatus.isDragging = isDragging
      this.mouseStatus.dragStartPosition = startPosition
    },

    // 设置鼠标滚轮增量
    setWheelDelta (delta) {
      this.mouseStatus.wheelDelta = delta
    },

    // 设置拾取对象
    setPickedObject (pickedObject) {
      this.mouseStatus.pickedObject = pickedObject
    },

    // 设置3D坐标
    setCartesian (cartesian) {
      this.mouseStatus.cartesian = cartesian
    },

    // 设置地理坐标
    setCartographic (cartographic) {
      this.mouseStatus.cartographic = cartographic
    },

    // 设置POI数据
    setPois (pois) {
      console.log('设置POI数据到 store ->', pois.length, '个POI')
      this.pois = Array.isArray(pois) ? pois : []
    },

    // 设置可见POI数据
    setVisiblePOIs (visiblePOIs) {
      console.log('设置可见POI数据到 store ->', visiblePOIs.length, '个可见POI')
      this.visiblePOIs = Array.isArray(visiblePOIs) ? visiblePOIs : []
    },

    // 设置瓦片加载状态
    setTileLoadingStatus (status) {
      this.tileLoadingStatus = { ...this.tileLoadingStatus, ...status }
    },

    // 开始加载瓦片
    startTileLoading (totalCount = 0) {
      this.tileLoadingStatus = {
        isLoading: true,
        loadedCount: 0,
        totalCount: totalCount,
        error: null
      }
    },

    // 完成瓦片加载
    completeTileLoading () {
      this.tileLoadingStatus.isLoading = false
      this.tileLoadingStatus.loadedCount = this.tiles.length
    },

    // 设置加载错误
    setTileLoadingError (error) {
      this.tileLoadingStatus.isLoading = false
      this.tileLoadingStatus.error = error
    },

    // 更新缓存统计信息
    updateCacheStats (stats) {
      this.cacheStats = { ...this.cacheStats, ...stats }
    },

    // 清除所有数据
    clearAllData () {
      console.log('清除 store 中的所有数据')
      this.tiles = []
      this.currentTileData = null
      this.tilePOIData = []
      this.viewportPOIData = []
      this.viewportCollisionAvoidedPOIData = []
      this.pois = []
      this.visiblePOIs = []
      this.screenHorizontalPosition = 0
      this.screenVerticalPosition = 0
      this.mapLevel = 0
      this.mouseStatus = {
        position: { x: 0, y: 0 },
        buttons: {
          left: false,
          middle: false,
          right: false
        },
        isDragging: false,
        dragStartPosition: null,
        wheelDelta: 0,
        pickedObject: null,
        cartesian: null,
        cartographic: null
      }
      this.tileLoadingStatus = {
        isLoading: false,
        loadedCount: 0,
        totalCount: 0,
        error: null
      }
    },

    // 根据ID查找POI
    findPOIById (id) {
      return this.pois.find(poi => poi.id === id)
    },

    // 根据类型获取POI
    getPOIsByType (type) {
      return this.pois.filter(poi => poi.type === type)
    },

    // 根据图层获取POI
    getPOIsByLayer (layer) {
      return this.pois.filter(poi => poi.layer === layer)
    },

    // 获取在指定边界内的POI
    getPOIsInBounds (bounds) {
      return this.pois.filter(poi => {
        return poi.longitude >= bounds.west &&
          poi.longitude <= bounds.east &&
          poi.latitude >= bounds.south &&
          poi.latitude <= bounds.north
      })
    },

    // 更新当前瓦片信息
    updateCurrentTileInfo (tileInfo) {
      this.currentTileInfo = { ...this.currentTileInfo, ...tileInfo }
    }
  }
})